package com.heima.behavior.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.heima.behavior.pojos.LikesBehaviorDto;
import com.heima.behavior.pojos.UnLikesBehaviorDto;
import com.heima.behavior.service.BehaviorService;
import com.heima.client.article.IArticleClient;
import com.heima.common.exception.CustomException;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.ArticleDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.thread.ApThreadLocalUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BehaviorServiceImpl implements BehaviorService {

    @Autowired
    private IArticleClient articleClient;

    @Autowired
    private CacheService cacheService;

    /**
     * 用户点赞行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likesBehavior(LikesBehaviorDto dto) {
        //校验
        if (dto == null || dto.getArticleId() == null){
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Integer userId = ApThreadLocalUtil.get();
        String userArticleId = String.valueOf(userId) + dto.getArticleId();
        //从redis中取出
        String jsonStr = cacheService.get(userArticleId);
        Map<String,Object> map = new HashMap<>();
        //判断是否存在数据
        if (StringUtils.isNotEmpty(jsonStr)){
            //获取数据
            map = JSONObject.parseObject(jsonStr, Map.class);
        }

        //判断是 0文章 1 动态 2 评论
        if (dto.getType() == 0){
            map.put("article",dto.getOperation());

        }else if (dto.getType() == 1){
            map.put("moments",dto.getOperation());

        }else if (dto.getType() == 2){
            map.put("comment",dto.getOperation());

        }
        //存入redis
        cacheService.set(userArticleId, JSON.toJSONString(map));
        //保存到apArticle表中
        ArticleDto articleDto = new ArticleDto();
        articleDto.setAuthorId(dto.getArticleId());
        articleDto.setLikes(dto.getOperation() == 0 ? 1:-1);
        articleClient.behaviorArticle(articleDto);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 阅读
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBehavior(LikesBehaviorDto dto) {
        if (dto == null || dto.getArticleId() == null){
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Integer userId = ApThreadLocalUtil.get();
        String userArticleId = String.valueOf(userId) + dto.getArticleId();
        //从redis中取出
        String jsonStr = cacheService.get(userArticleId);
        Map<String,Object> map = new HashMap<>();
        //判断是否存在数据
        if (StringUtils.isNotEmpty(jsonStr)){
            //获取数据
            map = JSONObject.parseObject(jsonStr, Map.class);
        }
        Object count1 = map.get("count");
        //判断是否浏览过
        if (count1 != null){
            int count = (int) count1;
            map.put("count",dto.getCount()+count);
        }else {
            map.put("count",dto.getCount());
        }

        //存入redis
        cacheService.set(userArticleId, JSON.toJSONString(map));
        //保存到apArticle表中
        ArticleDto articleDto = new ArticleDto();
        articleDto.setAuthorId(dto.getArticleId());
        articleDto.setViews(1);
        articleClient.behaviorArticle(articleDto);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 用户不喜欢
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLikesBehavior(UnLikesBehaviorDto dto) {
        if (dto == null || dto.getArticleId() == null){
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Integer userId = ApThreadLocalUtil.get();
        String userArticleId = String.valueOf(userId) + dto.getArticleId();
        //从redis中取出
        String jsonStr = cacheService.get(userArticleId);
        Map<String,Object> map = new HashMap<>();
        //判断是否存在数据
        if (StringUtils.isNotEmpty(jsonStr)){
            //获取数据
            map = JSONObject.parseObject(jsonStr, Map.class);
        }

        map.put("unlike", dto.getType());

        //存入redis
        cacheService.set(userArticleId, JSON.toJSONString(map));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
